﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Siser.Web.Models;
using Siser.Web.Authorization;


using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace Siser.Web.Controllers
{
    public class PruebaCorreoController : Controller
    {

        SiserEntities db = new SiserEntities();

        //
        // GET: /PruebaCorreo/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string x)
        {
            ViewBag.CorreoEnviado = Siser.Web.Shared.Mail.Send("siser.noreply@cchen.cl", "guerrero.niko@gmail.com", "CORREO SISER - NO RESPONDA", x, true, GetPDFStream()) == true ? "SI" : "NO";
            return View();
        }

        public byte[] GetPDFStream()
        {
            Document document = new Document(new Rectangle(460f, 400f));
            MemoryStream memoryStream = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);

            document.Open();
            document.Add(new Paragraph("SISER - Tarjeta de identificación \n\r", FontFactory.GetFont("Arial", 8)));
            document.Add(GeneraTablaPDF());
            writer.CloseStream = false;
            document.Close();

            byte[] stream = new byte[memoryStream.Position];
            memoryStream.Position = 0;
            memoryStream.Read(stream, 0, stream.Length);
            return stream;
        }
        public PdfPTable GeneraTablaPDF()
        {
            PdfPTable pdfTable = new PdfPTable(6);
            PdfPCell cell = new PdfPCell();
            int i = 1;

            pdfTable.SetWidthPercentage(new float[] { 80, 80, 80, 80, 80, 80 }, new Rectangle(480f, 100f));
            pdfTable.AddCell(new Paragraph("", FontFactory.GetFont("Arial", 8)));
            pdfTable.AddCell(new Paragraph("A", FontFactory.GetFont("Arial", 8)));
            pdfTable.AddCell(new Paragraph("B", FontFactory.GetFont("Arial", 8)));
            pdfTable.AddCell(new Paragraph("C", FontFactory.GetFont("Arial", 8)));
            pdfTable.AddCell(new Paragraph("D", FontFactory.GetFont("Arial", 8)));
            pdfTable.AddCell(new Paragraph("E", FontFactory.GetFont("Arial", 8)));

            foreach (PdfPCell item in pdfTable.Rows[0].GetCells())
            {
                item.BackgroundColor = BaseColor.LIGHT_GRAY;
                item.HorizontalAlignment = 1;
                item.Padding = 3;
            }

            var coordenadas = (from c in db.Coordenadas
                               join ti in db.TarjetasIdentificaciones on c.TarjetaIdentificacionId equals ti.Id
                               join u in db.Usuarios on ti.UsuarioId equals u.Id
                               where u.Nombre == HttpContext.User.Identity.Name
                                  && u.Vigencia == true
                                  && ti.Vigencia == true
                               select c).ToList();

            foreach (Coordenada coordenada in coordenadas)
            {
                if (coordenada.J == "A")
                {
                    cell = new PdfPCell(new Paragraph(i.ToString(), FontFactory.GetFont("Arial", 8)));
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    cell.HorizontalAlignment = 1;
                    pdfTable.AddCell(cell);
                    i++;
                }
                cell = new PdfPCell(new Paragraph(coordenada.Valor.ToString(), FontFactory.GetFont("Arial", 8)));
                cell.HorizontalAlignment = 1;
                pdfTable.AddCell(cell);
            }
            return pdfTable;
        }

    }
}
